#!/bin/bash
libs=(libavcodec.so.57 libavformat.so.57 libavutil.so.55 libavdevice.so.57)
for lib in ${libs[*]}
do
	result=`ldconfig -v 2>&1 | grep $lib`
	if [ -z "$result" ];
	then
		echo "Error 12: $lib Not Found!"
		exit 1
	fi
done


Transcoder="./transcoder"
State="Job";
Task_Id=0;
Route_Id=0;
Task=0;
Route=0;
Start_Core=0;
Cores=`nproc`;

declare -A Inputs;
declare -A Routes;
declare -A Start_Cores;

declare -A Video_Libs;
declare -A Rates;
declare -A Resolutions;
declare -A Audio_Libs;
declare -A Outputs;

while read line
do
	if [[ "$line" == "Task Begin" ]];
	then
		State="Task";
		let Route=0;
		let Route_id=0;
		Input="None";
		Route=0;
		continue;
	fi

	if [[ "$line" == "Route Begin" ]];
	then
		State="Route";
		c_v="None";
		b_v="None";
		s="None";
		c_a="None";
		Output="None";
		continue;
	fi

	if [[ "$line" == "Task End" ]];
	then
		State="None";
		if [[ "$Input" == "None" ]];
		then
			echo Error 15: Task $Task_Id must have input video
			exit 1;
		fi

		if [ "$Route" -eq 0 ];
		then
			echo Error 16: $Task_Id must have at least 1 route
			exit 1;
		fi

		Inputs[$Task_Id]=$Input;
		Routes[$Task_Id]=$Route;

		let Task_Id++;
		let Task++;

		continue;
	fi

	if [[ "$line" == "Route End" ]];
	then
		State="None";

		if [[ $c_v == "None" ]]; then
			echo Error 17: Task $Task_Id Route $Route_Id must has c_v parameter
			exit 1
		fi

		if [[ $b_v == "None" ]]; then
			echo Error 18: Task $Task_Id Route $Route_Id must has b_v parameter
			exit 1
		fi

		if [[ $s == "None" ]]; then
			echo Error 19: Task $Task_Id Route $Route_Id must has s parameter
			exit 1
		fi

		if [[ $c_a == "None" ]]; then
			echo Error 20: Task $Task_Id Route $Route_Id must has c_a parameter
			exit 1
		fi

		if [[ $Output == "None" ]]; then
			echo Error 20: Task $Task_Id Route $Route_Id must has Output parameter
			exit 1
		fi

		Video_Libs[$Route_Id]=$c_v;
		Rates[$Route_Id]=$b_v;
		Resolutions[$Route_Id]=$s;
		Audio_Libs[$Route_Id]=$c_a;
		Outputs[$Route_Id]=$Output;

		let Route_Id++;
		let Route++;
		continue;
	fi

	if [[ $State == "Job" ]];
	then
		eval "$line";
	fi

	if [[ $State == "Task" ]];
	then
		eval "$line";
	fi

	if [[ $State == "Route" ]];
	then
		eval "$line";
	fi
done < $1



if [ "$Cores" -lt "$Task" ]; then
	echo Error 13: Task amount $Task more than Core amount $Cores
	exit 1
fi

if [ "$Task" -lt 0 ]; then
	echo Error 14: Must at least run 1 Task
	exit 1
fi

echo $1
echo $2

#if [ $Task == 1 ]; then
	#fs=100
#else
	#fs=10
#fi

#fs=$3
#gs=$4

left_cores=`expr $Cores % $Task`;
echo $left_cores

for ((i=0; i<$Task; i++))
do
    let Threads[$i]=0;
    if [ "$left_cores" -gt 0 ]; then
	    Threads[$i]=`expr $Cores / $Task + 1`; 
    else
	    Threads[$i]=`expr $Cores / $Task`;
    fi
      
    left_cores=`expr $left_cores - 1`;

	Start_Cores[$i]=$Start_Core;

    gs=80;
    fs=$[${Threads[$i]}*2];

	# Command[$i]="time -p $Transcoder -i ${Inputs[$i]}";
	Command[$i]="time -p $Transcoder -i $2";
	Route=${Routes[$i]}

	for ((j=0; j<$Route; j++))
	do
		Command[$i]+=" -c:v ${Video_Libs[$(($i * $Route + $j))]} -b:v ${Rates[$(($i * $Route + $j))]} -s ${Resolutions[$(($i * $Route + $j))]} -c:a ${Audio_Libs[$(($i * $Route + $j))]} ${Outputs[$(($i * $Route + $j))]}";
	done
	Command[$i]+=" -threads ${Threads[$i]} -fc ${Start_Cores[$i]} -gs $gs -fs $fs";

	#echo ${Command[$i]};

	if [ $i -lt $(($Task - 1)) ];
	then
		Command[$i]+=" -quiet &";
	fi

    let Start_Core+=${Threads[$i]};
done

for ((i=0; i<$Task; i++))
do
	 echo ${Command[$i]};
	 eval ${Command[$i]};
done

wait

echo "###########################################################"
